package com.damn;


import java.io.InputStream;
import com.restfb.BinaryAttachment;
import com.restfb.Connection;
import com.restfb.DefaultFacebookClient;
import com.restfb.FacebookClient;
import com.restfb.Parameter;
import com.restfb.types.FacebookType;
import com.restfb.types.Post;

public class RestFB {	
	public FacebookClient fbClient = null;
	
	public String initFBClient(String token)	{
		String responseStr = "fail";
		try{
			fbClient = new DefaultFacebookClient(token);
			responseStr = "success";
		}
		catch(Exception e)	{
			e.printStackTrace();
		}
		System.out.println(token);
		return responseStr;
	}
	
	public String CreatePost(String FBMessage)		{		
		String responseStr = "";
		try{			
			fbClient.publish("me/feed", FacebookType.class, Parameter.with("message", FBMessage));
			responseStr = "success";
		}
		catch (Exception e)
		{
			e.printStackTrace();
			responseStr = "fail";
		}
		//this.getFeed();
		return responseStr;
	}	

	public String publishPhoto(InputStream picStream , String caption) {
		String responseStr = "";
		try{			        
			fbClient.publish("me/photos", FacebookType.class,
	          BinaryAttachment.with("test1.jpg", picStream),
	          Parameter.with("message", caption));
	        responseStr = "success";
		}
		catch(Exception e)
		{
			e.printStackTrace();
			responseStr = "fail";
		}
		return responseStr;
	  }
	
	public Connection<Post> getFeed()	{
		Connection<Post> feed = fbClient.fetchConnection("me/feed", Post.class);
		System.out.println(feed.getData().size());
		for(int index = 0;index<feed.getData().size();index++)	{
			System.out.println("-------------------------Feed------------------------");
			System.out.println(feed.getData().get(index).getAttribution());			
			System.out.println(feed.getData().get(index).getCaption());
			System.out.println(feed.getData().get(index).getDescription());
			System.out.println(feed.getData().get(index).getLikesCount());
			System.out.println(feed.getData().get(index).getMessage());
			System.out.println(feed.getData().get(index).getName());
			System.out.println(feed.getData().get(index).getType());
			System.out.println(feed.getData().get(index).getObjectId());
			System.out.println("-------------------------Feed------------------------");
		}		
		return feed;
	}
}

